﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBControl;
using System.Data;
using System.Web.Security;

namespace ProjectX
{
    public partial class Login : Errorbase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["userNick"] != null)
                {

                }
            }

        }


        protected void loginBtn_Click(object sender, ImageClickEventArgs e)
        {
            if (this.unamecstValidator.IsValid && this.pwdcstValidator.IsValid)
            {
                Session.Add("userNick", this.username.Text);
                Response.Redirect("/", false);
            }
        }

        protected void unamecstValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string getusername = "SELECT userName FROM UserRegister WHERE userName = '" + args.Value + "'";
            SqlDbHelper sdh = new SqlDbHelper();
            if (sdh.ExecuteDataTable(getusername).Rows.Count > 0)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        protected void pwdcstValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string userPossword = FormsAuthentication.HashPasswordForStoringInConfigFile(args.Value, "MD5");
            string getuser = "SELECT userName FROM UserRegister WHERE userName = '" + this.username.Text.Trim() + "' AND passWord='" + userPossword + "'";
            SqlDbHelper sdh = new SqlDbHelper();
            if (sdh.ExecuteDataTable(getuser).Rows.Count > 0)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        protected void getpwd_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("/getpass.html",false);
        }

        protected void regnew_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("/regedit.html", false);
        }
    }
}